package com.example.demo.module.DTO;

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.json.JSONUtil;
import com.example.demo.constant.BaseConstant;
import lombok.AllArgsConstructor;
import lombok.Data;
import lombok.NoArgsConstructor;

import java.io.Serializable;
import java.util.Map;

@Data
@AllArgsConstructor
@NoArgsConstructor
public class Datasource implements Serializable {

    private static final long serialVersionUID = 1L;

    private Long dsId;

    private Long userId;

    private Long orgId;

    private String dsName;

    private String dsType;

    private String typeDetil;

    private String ifValid;

    private String theDesc;

    private Integer tableCount;

    private String datasourceJson;

    private String businessCode;

    private String status;

    private String schemaName;

    public DatasourceDto toDatasourceDto() {
        DatasourceDto datasourceDto = BeanUtil.copyProperties(this, DatasourceDto.class);
        Map map = JSONUtil.toBean(this.datasourceJson, Map.class);
        datasourceDto.setUrl((String) map.get("url"));
        switch (this.getTypeDetil()) {
            case BaseConstant.MYSQL:
            case BaseConstant.ORACLE:
            case BaseConstant.POSTGRES:
            case BaseConstant.DM: {
                datasourceDto.setDriver((String) map.get("driver"));
                datasourceDto.setUsername((String) map.get("username"));
                datasourceDto.setPassword((String) map.get("password"));
                datasourceDto.setPrefixWhite((String) map.get("prefixWhite"));
                datasourceDto.setPrefixBlack((String) map.get("prefixBlack"));
                datasourceDto.setSuffixWhite((String) map.get("suffixWhite"));
                datasourceDto.setSuffixBlack((String) map.get("suffixBlack"));
                datasourceDto.setRegularFilter((String) map.get("regularFilter"));
                return datasourceDto;
            }
            case BaseConstant.HDFS: {
                return datasourceDto;
            }
            case BaseConstant.KAFKA: {
                datasourceDto.setGroupId((String) map.get("groupId"));
                datasourceDto.setTopic((String) map.get("topic"));
                datasourceDto.setEnableAutoCommit((String) map.get("enableAutoCommit"));
                datasourceDto.setAutoOffsetReset((String) map.get("autoOffsetReset"));
                datasourceDto.setConnectTime((String) map.get("connectTime"));
                return datasourceDto;
            }
            default: {
                return datasourceDto;
            }
        }
    }
}
